/**
 * @file combiprofit/Home.js
 *  组合盈亏
 * @author
 */

 import React, {Component, PropTypes} from 'react';
 import {connect} from 'dva';
 import {routerRedux, Link, withRouter} from 'dva/router';
 import {
   Button,
   Modal,
   Row,
   Col,
   Table,
   Icon,
   Card,
   Form,
   message
 } from 'antd';
 import moment from 'moment';
 import Tabtitle from '../../../components/common/TabTitle';
 import Tablelist from '../../../components/common/TableData';
 // import DateRangePicker from '../component/searchBar/Datesearch';
 import DateRangePicker from '../component/searchBar/DateSearchBar';
 import styles from './home.less';


 const nowDate = moment(new Date()).format('YYYYMMDD');
 const params = {
   table: 'ifcombiprofit',
   pagesize: 15,
   currentpage: 1
 };
 const Combiprofit = ({
   dispatch,
   combiprofit,
   location: {
     pathname,
     query
   }
 }) => {
   if (combiprofit.status) {
     const {combiprofitlist,currentpage, page, state, begindate, enddate,msg} = combiprofit;
     console.log('combiprofit>>>>', combiprofit,);
     const tag = "combiprofit";
     const columns = [
  {
    title: '交易日期',
    dataIndex: 'tradeDay',
    key: 'tradeDay',
    sorter: true,
  },
  {
    title: '基金号',
    dataIndex: 'fundId',
    key: 'fundId',
  },
  {
    title: '组合号',
    dataIndex: 'combiNo',
    key: 'combiNo',
    sorter: true,
  },
  {
    title: '组合名称',
    dataIndex: 'combiName',
    key: 'combiName',
  },
  {
    title: '组合总盈亏',
    dataIndex: 'totalProfit',
    key: 'totalProfit',
  },
  {
    title: '基差总盈亏',
    dataIndex: 'basisSpreadProfit',
    key: 'basisSpreadProfit',
  },
  {
    title: '贝塔盈亏',
    dataIndex: 'betaProfit',
    key: 'betaProfit',
  },
  {
    title: '成本',
    dataIndex: 'cost',
    key: 'cost',
  },
  {
    title: '阿尔法盈亏',
    dataIndex: 'alphaProfit',
    key: 'alphaProfit',
  },
  {
    title: '阿尔法盈亏+成本',
    dataIndex: 'alphaAndCost',
    key: 'alphaAndCost',
  },
  {
    title: '空头市值',
    dataIndex: 'totalShortValue',
    key: 'totalShortValue',
  },
  {
    title: '多头市值',
    dataIndex: 'totalLongValue',
    key: 'totalLongValue',
  },
];
     // 分页
     function onShowSizeChange(current, pagesize) {
       dispatch({
         type: 'combiprofit/changeRouter',
         payload: {
           table: 'ifcombiprofit',
           pagesize,
           currentpage:1,
           begindate: begindate,
           enddate: enddate,
           sortField: query.sortField,
            sortOrder: query.sortOrder,
         }
       })
     }
     function onChangecurrentpage(current,pagesize) {
       dispatch({
         type: 'combiprofit/changeRouter',
         payload: {
           table: 'ifcombiprofit',
           currentpage: current,
           pagesize:pagesize,
           begindate: begindate,
           enddate: enddate,
           sortField: query.sortField,
            sortOrder: query.sortOrder,
         }
       })
     }
     //排序
    function onSorterChange(pagesize, filters, sorter) {
       const orderField = sorter.field;
       const orderSort = sorter.order;
       console.log(sorter, "orderField>>", orderField, "orderSort", orderSort);
       if(orderField !== undefined && orderSort !== undefined){
         if(query.pagesize&&query.begindate&&query.enddate){
            dispatch({
             type: 'combiprofit/changeRouter',
             payload: {
               table: 'ifcombiprofit',
               currentpage: currentpage,
               pagesize:query.pagesize,
               sortField: orderField,
               sortOrder: orderSort,
               begindate: query.begindate,
               enddate: query.enddate
             }
           })
          }else{
            dispatch({
             type: 'combiprofit/changeRouter',
             payload: {
               table: 'ifcombiprofit',
               currentpage: currentpage,
               pagesize:15,
               begindate,
               enddate,
               sortField: orderField,
               sortOrder: orderSort,
             }
           })
          }
        }else{
          return null
        }
     }
    //导出
    function downLoad(){
      const nowBegindate = begindate || moment(new Date()).format('YYYYMMDD');
      const nowEnddate = enddate || moment(new Date()).format('YYYYMMDD');
      window.location.href=location.href.indexOf('localhost')>-1?`/api/ifdata/history/export?table=ifcombiprofit&begindate=${nowBegindate}&enddate=${nowEnddate}`:`/thfx/api/ifdata/history/export?table=ifcombiprofit&begindate=${nowBegindate}&enddate=${nowEnddate}`;
    }
    function queryData() {
    console.log('beginDate>>>',begindate,'endDate>>>',enddate)
    if(begindate == "" || enddate == "" || begindate == "Invalid date"){
      message.warning('请选择时间');
      return;
    }else{
      dispatch({
        type: `${tag}/changeRouter`,
        payload: {
          table:`if${tag}`,
          pagesize:15,
          currentpage:1,
          begindate:begindate || BD,
          enddate:enddate || ED
        }
      })
      dispatch({
        type: `${tag}/setState`,
        payload: {
          'isRefresh':0
        }
      })
    }
  }
     return (
       <div>
         <Tabtitle value={'组合盈亏'}/>
         <div className="page-combiprofit-home content-inner" >
           <Row className={styles.toolbar} type="flex" justify="start" align="middle">
             <Col span={4}>
               <DateRangePicker tag={tag} dispatch={dispatch} begindate={begindate} enddate={enddate} location={location}/>
             </Col>
             <Col span={18} push={2}>
              <Button type="primary" onClick={queryData}>
                查询
              </Button>
            </Col>
             <Col span={2}>
               <Button type="primary" onClick={downLoad}>导出</Button>
             </Col>
           </Row>
           
             <Tablelist columns={columns} data={combiprofitlist} page={page} onShowSizeChange={onShowSizeChange} onChangecurrentpage={onChangecurrentpage} onSorterChange={onSorterChange} record={record => `${record.tradeDay}-${record.fundId}-${record.combiNo}-${record.combiName}-${record.totalProfit}-${record.basisSpreadProfit}-${record.betaProfit}-${record.cost}-${record.alphaProfit}-${record.alphaAndCost}-${record.totalShortValue}-${record.totalLongValue}`}/>
           
         </div>
       </div>
     )
   } else {
     return null
   }
 }

 Combiprofit.propTypes = {}
 export default connect(({combiprofit}) => ({combiprofit}))(Combiprofit)
